﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using ini;

namespace InIReaderExample
{
	public partial class FrmCopySections : Form
	{
		private readonly Dictionary<string, InIReader> reader;

		/// <summary>
		///     Initializes a new instance of the <see cref="FrmCopySections" /> class.
		/// </summary>
		/// <param name="reader">The reader.</param>
		public FrmCopySections(Dictionary<string, InIReader> reader)
		{
			InitializeComponent();

			this.reader = reader;

			string[] names = reader.Keys.ToArray();
			cbFile.Items.AddRange(names);
			cbFile.AutoCompleteCustomSource.AddRange(names);

			foreach (var pair in reader)
			{
				TreeNode file = new TreeNode(pair.Key);
				tvSections.Nodes.Add(file);
				file.Tag = pair.Value;

				foreach (var section in pair.Value)
				{
					TreeNode sec = new TreeNode(section.Key);
					sec.Tag = section.Value;
					sec.ToolTipText = section.Value.ToString();
					file.Nodes.Add(sec);
				}
			}
		}

		/// <summary>
		///     BTNs the cancel click.
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">
		///     The <see cref="EventArgs" /> instance containing the event data.
		/// </param>
		private void BtnCancelClick(object sender, EventArgs e)
		{
			DialogResult = DialogResult.Cancel;
			Close();
		}

		/// <summary>
		///     BTNs the ok click.
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">
		///     The <see cref="EventArgs" /> instance containing the event data.
		/// </param>
		private void BtnOkClick(object sender, EventArgs e)
		{
			CopySections();

			DialogResult = DialogResult.OK;
			Close();
		}

		/// <summary>
		/// Copies the sections.
		/// </summary>
		private void CopySections()
		{
			InIReader tmp = reader[cbFile.Text];

			foreach (TreeNode node in tvSections.Nodes)
			{
				foreach (TreeNode child in node.Nodes)
				{
					if (child.Checked)
					{
						tmp[child.Text].Add((InISection)child.Tag, cbReplace.Checked);
					}
				}
			}
		}

		/// <summary>
		/// Handles the AfterCheck event of the tvSections control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="TreeViewEventArgs" /> instance containing the event data.</param>
		private void TvSectionsAfterCheck(object sender, TreeViewEventArgs e)
		{
			CheckChildNodes(e.Node);
		}

		private void CheckChildNodes(TreeNode node)
		{
			foreach (TreeNode child in node.Nodes)
			{
				child.Checked = node.Checked;

				if(child.Nodes.Count > 0)
					CheckChildNodes(child);
			}
		}

		/// <summary>
		/// Cbs the file text changed.
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
		private void CbFileTextChanged(object sender, EventArgs e)
		{
			btnOk.Enabled = !String.IsNullOrWhiteSpace(cbFile.Text) &&
			                reader.ContainsKey(cbFile.Text);
		}
	}
}